/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject.selectQuestion;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import ru.irenproject.FormulationSupplier;
import ru.irenproject.Question;
import ru.irenproject.TestCheck;
import ru.irenproject.pad.Pad;
import ru.irenproject.store.AbstractThing;
import ru.irenproject.store.Event;
import ru.irenproject.store.Stash;

import com.google.protobuf.ByteString;

public final class SelectQuestion extends AbstractThing
    implements Question, FormulationSupplier {
  public enum ChangeEvent implements Event { INSTANCE }

  private static final int MAX_CHOICES = 20;

  @Stash private SelectQuestionProtos.SelectQuestion.Builder $;

  @Inject SelectQuestion() {}

  @Override protected void initialize() {
    Pad formulation = store().create(Pad.class);
    $.setFormulation(formulation.id());
  }

  @Override public Pad formulation() {
    return get($.getFormulation(), Pad.class);
  }

  public SelectQuestionChoice addChoice(boolean negative) {
    TestCheck.input(canAddChoice());
    if (negative) {
      TestCheck.input(!hasNegativeChoice());
    }

    SelectQuestionChoice res = SelectQuestionChoice.create(store(), negative);
    $.addChoice(res.id());
    post(ChangeEvent.INSTANCE);

    return res;
  }

  public boolean canAddChoice() {
    return choiceCount() < MAX_CHOICES;
  }

  public List<SelectQuestionChoice> choiceList() {
    return store().getBatch($.getChoiceList(), SelectQuestionChoice.class);
  }

  public int choiceCount() {
    return $.getChoiceCount();
  }

  public void deleteChoice(int choiceIndex) {
    checkChoiceIndex(choiceIndex);

    List<ByteString> ids = new ArrayList<>($.getChoiceList());
    ids.remove(choiceIndex);
    $.clearChoice().addAllChoice(ids);

    post(ChangeEvent.INSTANCE);
  }

  private boolean validChoiceIndex(int index) {
    return (index >= 0) && (index < choiceCount());
  }

  private void checkChoiceIndex(int index) {
    TestCheck.input(validChoiceIndex(index));
  }

  private boolean canMoveChoice(int index, boolean forward) {
    return validChoiceIndex(index) && validChoiceIndex(index + (forward ? 1 : -1));
  }

  public void moveChoice(int index, boolean forward) {
    TestCheck.input(canMoveChoice(index, forward));

    int newIndex = index + (forward ? 1 : -1);
    ByteString temp = $.getChoice(index);
    $.setChoice(index, $.getChoice(newIndex));
    $.setChoice(newIndex, temp);

    post(ChangeEvent.INSTANCE);
  }

  public boolean hasNegativeChoice() {
    for (SelectQuestionChoice c : choiceList()) {
      if (c.negative()) {
        return true;
      }
    }
    return false;
  }

  @Override public void setDefaults(String initialText) {
    formulation().appendText(initialText);
  }
}
